package org.szgb.console.sys.custom.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.sys.custom.entity.SysCustomForm;
import org.szgb.console.sys.custom.mapper.SysCustomFormMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class SysCustomFormService {
    private final SysCustomFormMapper sysCustomFormMapper;

    @Autowired
    public SysCustomFormService(SysCustomFormMapper sysCustomFormMapper){
        this.sysCustomFormMapper = sysCustomFormMapper;
    }

    @Transactional(readOnly = true)
    public SysCustomForm get(String id) {
        return sysCustomFormMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<SysCustomForm> findList(SysCustomForm sysCustomForm) {
        if (sysCustomForm != null && sysCustomForm.getPageDto() != null) {
            Page<SysCustomForm> page = new Page<>(sysCustomForm.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            sysCustomForm.setPage(page);
        }
        return sysCustomFormMapper.findList(sysCustomForm);
    }

    @Transactional(readOnly = true)
    public Integer getCount(SysCustomForm sysCustomForm) {
        return sysCustomFormMapper.getCount(sysCustomForm);
    }

    @Transactional()
    public void save(SysCustomForm sysCustomForm) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (sysCustomForm.getId() == null || sysCustomForm.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            sysCustomForm.setCreatedBy(sysCustomForm.getUpdatedBy());
            // 添加UUID
            sysCustomForm.setId(UUIDUtil.getUUID());
            // 执行插入操作
            sysCustomFormMapper.insert(sysCustomForm);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            sysCustomFormMapper.update(sysCustomForm);
        }
    }

    @Transactional
    public void delete(String ids) {
        sysCustomFormMapper.delete(ids.split(","));
    }
}
